
open System.IO

let sortAndCountComparisons(xs: int[]) = 
    let mutable comparisons = 0
    let rec partition(ys: int[], start, length) =
        if length > 1 
        then
            let test = Array.sub ys start length
            comparisons <- comparisons + length - 1
            let pivot = ys.[0]
            let i = ref start
            while (!i < start + length && ys.[!i] <= pivot) do incr i
            let j = ref(!i + 1)
            while(!j < length) do
                if (ys.[!j] <= pivot)
                then 
                    let tmp = ys.[!j]
                    ys.[!j] <- ys.[!i]
                    ys.[!i] <- tmp
                    incr i

                incr j

            ys.[0] <- ys.[!i-1]
            ys.[!i-1] <- pivot
            partition(ys, 0, !i)
            partition(ys, !i + 1, length - (!i + 1))
            
    partition(xs, 0, xs.Length)

    comparisons

let mutable input = [| 4; 3; 6; 2; 1; 5 |]
sortAndCountComparisons(input) 

input <- File.ReadAllLines( __SOURCE_DIRECTORY__ + "\prog.assigment.input.txt") |> Array.map int
sortAndCountComparisons input





https://management.azure.com/subscriptions/882baac0-4961-4015-9230-61925f86c6ac/resourceGroups/WebProd/providers/Microsoft.Sql/servers/web-prod-db/importExportOperationResults/5c105a9e-e2e7-4dba-aaf0-07fa5a25ed52?api-version=2014-04-01-Preview